<template>
  <van-button v-if="isFollowed"
              class="follow-btn"
              type="default"
              round
              size="small"
              :loading="loading"
              @click="onFollow">已关注</van-button>
  <van-button v-else
              class="follow-btn"
              type="info"
              color="#3296fa"
              round
              size="small"
              icon="plus"
              :loading="loading"
              @click="onFollow">关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  model: {
    prop: 'isFollowed',
    event: 'update-is_followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
          const { data } = await deleteFollow(this.userId)
          console.log(data)
        } else {
          const { data } = await addFollow(this.userId)
          console.log(data)
        }
        // this.article.isFollowed = !this.article.isFollowed
        this.$emit('update-is_followed', !this.isFollowed)
      } catch (err) {
        console.log(err.response)
        let message = '操作失败，请重试'
        if (err.response && err.response.stattus === 400) {
          message = '你不能关注你自己'
        }
        this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style>
</style>
